package com.medata.utils;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.medata.dataObject.Globalsetting;
import com.medata.exeptions.YunExceptions;
import com.medata.service.GlobalsettingService;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import javax.xml.namespace.QName;


/**
 * @author ：zb
 * @date ：Created in 2019/5/29 0029 19:02
 * @description：
 */

public class SolrCrudUtil {
    public static JsonObject simp_search(GlobalsettingService globalsettingService,String str) {
        Globalsetting globalsetting = globalsettingService.findByGsetname("IndexServerPath");
        String indexPath = globalsetting.getGsetvalue();
        String endPoint = indexPath + "/services/WebService?wsdl";
        String resultstr = "";
        try {
            RPCServiceClient client=new RPCServiceClient();
            Options options = client.getOptions();
            options.setTo(new EndpointReference(endPoint));
            options.setTimeOutInMilliSeconds(1000*60*5);
            options.setUserName("tsk");
            options.setPassword("123456");
            options.setAction("services");
            Object[] objects = client.invokeBlocking(new QName("searchWebService"), new Object[]{str}, new Class[]{String.class});
            resultstr = (String) objects[0];
            XmlUtil.getUnreplace(resultstr);
            //20190910zb修复字符的问题
//            resultstr=XmlUtil.getUnreplace(resultstr);
            JsonParser parser = new JsonParser();
            JsonObject returns = parser.parse(resultstr).getAsJsonObject();
            String returnflag = returns.get("returnflag").getAsString();
            if (!returnflag.equals("0")) {
                throw new YunExceptions(0, "检索单库的时候失败");
            }
            return returns;
        } catch (Exception e) {
            e.printStackTrace();
            throw new YunExceptions(0, "检索单库的时候出现bug");
        }
    }

    public static void main(String[] args) {
//        simp_search("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
//                "<parameter><action>ADVSEARCH</action><isred>0</isred><dbid>3</dbid><changeparameter>3</changeparameter><pagedown>0</pagedown><returnfield> <fieldname desc=\"客户名称\">customer</fieldname> <fieldname desc=\"证照类型\">licencetype</fieldname> <fieldname desc=\"证照名称\">licencename</fieldname> <fieldname desc=\"获得时间\">gettime</fieldname> <fieldname desc=\"有效期\">validperiod</fieldname> <fieldname desc=\"原件数量\">quantity</fieldname> <fieldname desc=\"原件编号\">originalnum</fieldname> <fieldname desc=\"证照标签\">licencelabel</fieldname> <fieldname desc=\"客户编号\">customernum</fieldname><fieldname desc=\"是否有文件\">hasobject</fieldname></returnfield><searchfield><advfield><field name=\"customer\" relation=\"and\" ishotkeyword=\"0\" searchkeyword=\"郑波\">郑波</field></advfield><filterfield/><ranfield/></searchfield><sortfield><field name=\"ID\" ></field></sortfield><curpage>1</curpage><pagenum>10</pagenum></parameter>"
//
//
//
//        );
    }
}
